package com.zxx.ssm.controller;

import com.zxx.ssm.dao.Student;
import com.zxx.ssm.model.biz.StuAvaTimeBiz;
import com.zxx.ssm.model.biz.StuAvaTimeblockBiz;
import com.zxx.ssm.model.convertor.StuAvaTimeConvertor;
import com.zxx.ssm.model.result.BizResponse;
import com.zxx.ssm.service.StuAvaTimeService;
import com.zxx.ssm.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("stuAvaTime")
public class StuAvaTimeController {

    @Autowired
    private StuAvaTimeService stuAvaTimeService;

    @RequestMapping("getAll")
    @ResponseBody
    public BizResponse getAll(@RequestParam String username){
        List<StuAvaTimeBiz> stuAvaTimeBizs = stuAvaTimeService.getAll(username);
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("stuAvaTime", stuAvaTimeBizs);
        return bizResponse;
    }

    @RequestMapping(value = "getAvaTimeStu", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse getAvaTimeStu(@RequestParam String currentDate){
        Date curDay = DateUtils.toDate(currentDate);
        Integer timeblockId = StuAvaTimeConvertor.convToTimeblock(currentDate);
        List<Student> studentList = stuAvaTimeService.getAvaTimeStu(curDay, timeblockId);
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("stuList", studentList);
        return bizResponse;
    }

    @RequestMapping(value = "getStuAvaTime", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse getStuAvaTime(@RequestParam String currentDate, @RequestParam Integer stuId){
        Date curDate = DateUtils.toDate(currentDate);
        Date firstDay = DateUtils.getFirstDayOfMonth(curDate);
        Date lastDay = DateUtils.getLastDayInMonth(curDate);
        List<StuAvaTimeblockBiz> stuAvaTimeBizs = stuAvaTimeService.getStuAvaTime(stuId, firstDay, lastDay);
        BizResponse response = new BizResponse();
        response.put("stuAvaTimeBizs", stuAvaTimeBizs);
        return response;
    }

    @RequestMapping(value = "getStuAvaTimeRange", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse getStuAvaTimeRange(@RequestParam String timeStart, @RequestParam String timeEnd, @RequestParam Integer stuId){
        Date startDay = DateUtils.toDate(timeStart);
        Date endDay = DateUtils.toDate(timeEnd);
        List<StuAvaTimeblockBiz> stuAvaTimeBizs = stuAvaTimeService.getStuAvaTime(stuId, startDay, endDay);
        BizResponse response = new BizResponse();
        response.put("stuAvaTimeBizs", stuAvaTimeBizs);
        return response;
    }
}
